﻿<%@page language="java" contentType="text/javascript; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import='java.sql.*' %>
<%@page import="java.util.ArrayList"%>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONSerializer"%>
<%@page import="net.sf.json.JSONArray"%>

<%String callback = request.getParameter("callback");%>
<%=callback %> (<%
	
	String searchvalue = new String(request.getParameter("searchvalue").getBytes("8859_1"), "utf-8");	
		
	Connection conn = null;
	PreparedStatement pstmt = null;
    
    JSONObject jo = new JSONObject();
    JSONArray ja = new JSONArray();
    
    
    try {
		Class.forName("com.mysql.jdbc.Driver");
		conn = DriverManager.getConnection("jdbc:mysql://localhost/itscreater", "itscreater", "campus123");
		
		String select = "select * from Post where (Title like ? or Content like ?);";
		pstmt = conn.prepareStatement(select);
		pstmt.setString(1, "%"+searchvalue+"%");
		pstmt.setString(2, "%"+searchvalue+"%");
		ResultSet rs = pstmt.executeQuery();
		
		while(rs.next()){
			/*String Title = rs.getString("title");
			JSONObject jo = new JSONObject();
			jo.put("TITLE",Title);
			JSONArray ja = new JSONArray();
			ja.add(jo);
			out.print(ja);*/ 
			//[{"TITLE":"hello~"}][{"TITLE":"I want to meet you!"}][{"TITLE":"Nice to meet you!!"}][{"TITLE":"에펠탑근처에서 맥주한잔 하실분~"}]
			
			/*String Title = rs.getString("title");
			JSONObject jo = new JSONObject();
			jo.put("TITLE", Title);
			out.print(jo);*/
			//{"TITLE":"hello~"}{"TITLE":"I want to meet you!"}{"TITLE":"Nice to meet you!!"}{"TITLE":"에펠탑근처에서 맥주한잔 하실분~"}
			String num = rs.getString("Number");
			String id = rs.getString("ID");
			String writer = rs.getString("Writer");
			String title = rs.getString("Title");
			String content = rs.getString("Content");
			if (title != null) {
				title = title.replace("\n","<br>");
				title = title.replace("\u0020","&nbsp;");
			}
			if (content != null) {
				content = content.replace("\n","<br>");
				content = content.replace("\u0020","&nbsp;");
			}
			String date = rs.getString("Date");
			String time = rs.getString("Time");
			String hhmm = time.substring(0, 5);
			
			jo.put("Number", num);
			jo.put("ID", id);
			jo.put("WRITER", writer);
			jo.put("TITLE", title);
			jo.put("CONTENT", content);
			jo.put("DATE", date);
			jo.put("TIME", hhmm);
			ja.add(jo);	
			}
		out.print(ja);
    }
    
	finally {
        try { 
            pstmt.close();
        } 
        catch (Exception ignored) {
        }
        try { 
            conn.close();
        } 
        catch (Exception ignored) {
        }
    }
%>);